/*
 * =====================================================================================
 *
 *       Filename:  config_openwrt.c
 *
 *    Description:  
 *
 *        Version:  1.0
 *        Created:  2019年11月29日 16时46分29秒
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  YOUR NAME (), 
 *   Organization:  
 *
 * =====================================================================================
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <cJSON.h>
#include "config.h"
#include "util_debug.h"

static int ubus_cjson_get_net_up(char *buff)
{
	if(NULL == buff || 0 == buff[0]) {
		return -1;
	}
	int ret = -1;
	cJSON *root = NULL;
	root = cJSON_Parse(buff);
	if(NULL == root) {
		return -1;
	}
	cJSON *up = cJSON_GetObjectItem(root, "up");
	if(NULL == up) {
		goto this_ret;
	}
    
    ret = up->valueint; 

this_ret:
	cJSON_Delete(root);
	return ret;
}

static int ubus_cjson_get_net_gateway(char *gateway, int len, const char *buff)
{
	if(NULL == buff || 0 == buff[0]) {
		return -1;
	}

	int i;
	int ret = -1;
	cJSON *root = NULL;
	root = cJSON_Parse(buff);
	if(NULL == root) {
		return -1;
	}
	cJSON *route_array = cJSON_GetObjectItem(root, "route");

	int size = cJSON_GetArraySize(route_array);
	if(size < 1) {
		//debug_print("route_array length < 1\n");
        ret = -1;
        goto this_return;
	}

	for(i = 0; i < size; i++) {
		cJSON *route = cJSON_GetArrayItem(route_array, i);
		if(NULL == route) {
			continue;
		}
		cJSON *target = cJSON_GetObjectItem(route, "target");
		cJSON *nexthop = cJSON_GetObjectItem(route, "nexthop");
		if(target && nexthop && 
		   cJSON_GetStringValue(target) && 
		   cJSON_GetStringValue(nexthop) &&
		   (0 == strcmp("0.0.0.0", cJSON_GetStringValue(target)))) {
			strcpy(gateway, cJSON_GetStringValue(nexthop));
			//debug_print("GateWay: %s\n", gateway);
            ret = 0;
            goto this_return;
		}
	}
	ret = -1;

this_return:

	cJSON_Delete(root);
	return ret;
}



static int ubus_cjson_get_net_interface(char *iface, int len, const char *buff)
{
	if(NULL == buff || 0 == buff[0]) {
		return -1;
	}
	int ret = -1;
	cJSON *root = NULL;
	root = cJSON_Parse(buff);
	if(NULL == root) {
		return -1;
	}
	cJSON *l3_device = cJSON_GetObjectItem(root, "l3_device");
	if(NULL == l3_device || NULL == cJSON_GetStringValue(l3_device)) {
		goto this_ret;
	}
	strcpy(iface, cJSON_GetStringValue(l3_device));
	ret = 0;

this_ret:
	cJSON_Delete(root);
	return ret;
}

int ubus_get_net_up(const char *net_name)
{
	char cmd[128] = {0};
	sprintf(cmd, "ubus call network.interface.%s status", net_name);
	//printf("Execute: [%s]\n", cmd);
	FILE *fp = popen(cmd, "r");
	if(NULL == fp) {
		debug_err("%s() popen(%s): %s\n", __func__, cmd, strerror(errno));
		return -1;
	}
	char buff[8192] = {0};
	fread(buff, 1, sizeof(buff), fp);
	pclose(fp);
	return ubus_cjson_get_net_up(buff);
}

int ubus_get_net_gateway(const char *net_name, char *gateway, int len)
{
	char cmd[128] = {0};
	sprintf(cmd, "ubus call network.interface.%s status", net_name);
	//printf("Execute: [%s]\n", cmd);
	FILE *fp = popen(cmd, "r");
	if(NULL == fp) {
		debug_err("%s() popen(%s): %s\n", __func__, cmd, strerror(errno));
		return -1;
	}
	char buff[8192] = {0};
	fread(buff, 1, sizeof(buff), fp);
	pclose(fp);
	return ubus_cjson_get_net_gateway(gateway, len, buff);
}

int ubus_get_net_interface(const char *net_name, char *iface, int len)
{
	char cmd[128] = {0};
	sprintf(cmd, "ubus call network.interface.%s status", net_name);
	//printf("Execute: [%s]\n", cmd);
	FILE *fp = popen(cmd, "r");
	if(NULL == fp) {
		debug_err("%s() popen(%s): %s\n", __func__, cmd, strerror(errno));
		return -1;
	}
	char buff[8192] = {0};
	fread(buff, 1, sizeof(buff), fp);
	pclose(fp);
	return ubus_cjson_get_net_interface(iface, len, buff);
}

